// pages/periphery/index.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        nav_for: [
            '景点',
            '美食',
            '酒店',
            // '公厕'
        ],
        longitude: null,
        latitude: null,
        markers: [
            // {
            //     id: 0,
            //     iconPath: "../index/image/picture.jpg",
            //     latitude: null,
            //     longitude: null,
            //     width: 50,
            //     height: 50
            // }
        ],
        // peripheryItem: [
        //     '../index/image/picture.jpg',
        //     '../index/image/picture.jpg',
        //     '../index/image/picture.jpg',
        //     '../index/image/picture.jpg'
        // ]
    },
    active(e) {
        // console.log(e)
        let _this = this
        let id = e.currentTarget.dataset["id"]

        if (id && id == 0) {
            console.log('asdfasdfasdf')
            _this.searchSomething("景点", 0)
        }
        else if (id && id == 1) {
            _this.searchSomething("美食", 1)
        } else{
            _this.searchSomething("酒店", 2)
        }
        // else {
        //     _this.searchSomething("公测", 3)
        // }
        this.setData({
            active: e.currentTarget.id
        })
    },
    searchSomething(search, type) {
        let _this = this
        let key = app.globalData.mapKey
        let latitude = app.globalData.location.latitude
        let longitude = app.globalData.location.longitude
        console.log()
        wx.request({
            url: `https://apis.map.qq.com/ws/place/v1/search?keyword=${search}&boundary=nearby(${latitude},${longitude},5000)&key=${key}`,
            data: {},
            header: { 'content-type': 'application/json' },
            method: 'GET',
            dataType: 'json',
            success: (res) => {
                // console.log(res.data.data)
                _this.makeMarker(res.data.data, type)
            },
            fail: () => {
                wx.showToast({
                    title: '请求失败！！！！',
                    icon: 'fail',
                    image: '',
                    duration: 1500,
                    success: (result) => {

                    }
                });
            }
        });
    },
    makeMarker(arr, type) {
        let _this = this
        console.log(arr)
        let markers = []
        if (type == 0) {
            arr.forEach(ele => {
                markers.push({
                    iconPath: 'image/view.png',
                    id: ele.id,
                    latitude: ele.location.lat,
                    longitude: ele.location.lng,
                    width: 25,
                    height: 25,
                    callout: {
                        content: 'asdfasdf',
                        padding: 5,
                        color: "#000",
                        borderRadius: 5,
                        bgColor: "#fff",
                        fontSize: '15',
                        display:"BYCLICK"
                    },
                })

            });
        }
        else if(type == 1){
            arr.forEach(ele => {
                markers.push({
                    iconPath: 'image/food.png',
                    id: ele.id,
                    latitude: ele.location.lat,
                    longitude: ele.location.lng,
                    width: 25,
                    height: 25,
                    callout: {
                        content: 'asdfasdf',
                        padding: 5,
                        color: "#000",
                        borderRadius: 5,
                        bgColor: "#fff",
                        fontSize: '15',
                        display:"BYCLICK"
                    },
                })
            });
        }else if(type == 2){
            arr.forEach(ele => {
                markers.push({
                    iconPath: 'image/building.png',
                    id: ele.id,
                    latitude: ele.location.lat,
                    longitude: ele.location.lng,
                    width: 25,
                    height: 25,
                    callout: {
                        content: 'asdfasdf',
                        padding: 5,
                        color: "#000",
                        borderRadius: 5,
                        bgColor: "#fff",
                        fontSize: '15',
                        display:"BYCLICK"
                    },
                })
            });
        }else{
            console.log('asdfasdfasdf')
        }
        _this.setData({
            markers
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        console.log(app.globalData)
        let latitude = app.globalData.location.latitude
        let longitude = app.globalData.location.longitude
        that.setData({
            latitude,
            longitude,
            markers: [{
                latitude,
                longitude
            }]
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
